/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bm.util;

import com.bm.main.Main;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultButtonModel;
import javax.swing.DefaultListModel;
import javax.swing.JRadioButton;

/**
 *
 * @author BimasaktiEmpat
 */
public class Fungsi {
    
    
    public static String getJarFolder() {
//        return "D:\\Projects\\D2D\\Projects\\Sistem Pendaftaran VRE\\Hasil\\MI_Text_SMS_Registrasi\\";
        try {
            String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            String decodedPath = URLDecoder.decode(path, "UTF-8");

            decodedPath = decodedPath.replace('/', File.separatorChar);
            if (decodedPath.indexOf(".jar") != -1) {
                decodedPath = decodedPath.substring(0, decodedPath.indexOf(".jar") + 4);
                decodedPath = decodedPath.substring(decodedPath.lastIndexOf(':') - 1);

                decodedPath = decodedPath.substring(0, decodedPath.lastIndexOf(File.separatorChar) + 1);

                System.out.println("Jar Folder : " + decodedPath);
            }
            return decodedPath;
        } catch (UnsupportedEncodingException ex) {
            System.out.println("Error Encoding path folder for properties file");
        }

        return null;
    }
    
    public static String getDetailTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");        
        return sdf.format(Calendar.getInstance().getTime());
    }

    public static String addchr(String string, int len, char kar, String justified) {
        StringBuffer ret = new StringBuffer(string);
        int kurang = len - string.length();
        if (justified.equalsIgnoreCase("right")) {
            for (int i = 0; i < kurang; i++) {
                ret = ret.insert(0, kar);
            }
        } else if (justified.equalsIgnoreCase("left")) {
            for (int i = 0; i < kurang; i++) {
                ret = ret.append(kar);
            }
        }
        return ret.toString();
    }

    public static boolean in_array(String[] haystack, String needle) {
        for (int i = 0; i < haystack.length; i++) {
            if (haystack[i].equals(needle)) {
                return true;
            }
        }
        return false;
    }

    public static boolean in_array(DefaultListModel haystack, String needle) {
        for (int i = 0; i < haystack.size(); i++) {
            if (haystack.get(i).toString().equals(needle)) {
                return true;
            }
        }
        return false;
    }

    public static String implode(String[] haystack, String glue) {
        String ret;
        if (haystack.length == 0) {
            ret = "";
        } else {
            StringBuffer sb = new StringBuffer();
            sb.append(haystack[0]);
            for (int i = 1; i < haystack.length; i++) {
                sb.append(glue);
                sb.append(haystack[i]);
            }
            ret = sb.toString();
        }
        return ret;
    }

    public static boolean isSelected(JRadioButton btn) {
        DefaultButtonModel model = (DefaultButtonModel) btn.getModel();
        return model.getGroup().isSelected(model);
    }

    public static String formatWaktu(Date waktu, String fmt) {
        Format formatter;
        if (fmt == null) {
            fmt = "dd/MM HH:mm:ss";
        }
        formatter = new SimpleDateFormat(fmt);

        return formatter.format(waktu);
    }

    public static String formatWaktuISO(Date waktu) {
        Format formatter;
        formatter = new SimpleDateFormat("HHmmss");

        return formatter.format(waktu);
    }

    public static String formatTglISO(Date tgl) {
        Format formatter;
        formatter = new SimpleDateFormat("MMdd");

        return formatter.format(tgl);
    }

    public static String getDateSettlement(Date tgl) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(tgl);
        DateFormat dateFormat = new SimpleDateFormat("MMdd");
        cal.add(Calendar.DATE, 1);

        return dateFormat.format(cal.getTime());
    }

    public static String formatTglWaktuISO(Date tgl) {
        Format formatter;
        formatter = new SimpleDateFormat("MMddHHmmss");

        return formatter.format(tgl);
    }

    public static String formatTglWaktuISO(String tgl) {
        if (tgl == null) {
            Date tanggal = new Date();
            Format formatter;
            formatter = new SimpleDateFormat("MMddHHmmss");

            return formatter.format(tanggal);
        }
        String bulan = tgl.substring(5, 7);
        String hari = tgl.substring(8, 10);
        String jam = tgl.substring(11, 13);
        String menit = tgl.substring(14, 16);
        String detik = tgl.substring(17, 19);

        StringBuffer ret = new StringBuffer();
        ret.append(bulan).append(hari).append(jam).append(menit).append(detik);

        return ret.toString();
    }

    public static String formatTanggal(Date tgl, String fmt) {
        Format formatter;
        if (fmt == null) {
            fmt = "yyyy-MM-dd";
        }
        formatter = new SimpleDateFormat(fmt);

        return formatter.format(tgl);
    }

    public static String formatTanggalCoreFMSS(Date tgl, String fmt) {
        Format formatter;
        fmt = "yyyy-MM-dd HH:mm:ss";
        formatter = new SimpleDateFormat(fmt);
        return formatter.format(tgl);
    }

    public static String formatUang(double nilai) {
        String ret = Double.toString(nilai);
        NumberFormat nf = NumberFormat.getInstance();
        ret = nf.format(nilai);
        return ret;
    }

    public static String formatUang(int nilai) {
        String ret = Integer.toString(nilai);
        NumberFormat nf = NumberFormat.getInstance();
        ret = nf.format(nilai);
        return ret;
    }

    public static long getSelisihTgl(Date tgl_akhir, Date tgl_awal, int satuan) {
        long ret = 0;
        Calendar c1 = Calendar.getInstance(); //new GregorianCalendar();
        Calendar c2 = Calendar.getInstance(); //new GregorianCalendar();
        c1.set(tgl_akhir.getYear(), tgl_akhir.getMonth(), tgl_akhir.getDate(), tgl_akhir.getHours(), tgl_akhir.getMinutes(), tgl_akhir.getSeconds());
        c2.set(tgl_awal.getYear(), tgl_awal.getMonth(), tgl_awal.getDate(), tgl_awal.getHours(), tgl_awal.getMinutes(), tgl_awal.getSeconds());

        long hasil = (c1.getTime().getTime() - c2.getTime().getTime());
        switch (satuan) {
            case 0: //detik
                ret = hasil / 1000;
                break;
            case 1: //menit
                ret = hasil / (60 * 1000);
                break;
            case 2: //jam
                ret = hasil / (3600 * 1000);
                break;
            case 3: //hari
                ret = hasil / (24 * 3600 * 1000);
                break;
            default:
                break;
        }
        return ret;
    }

    public static String getString(String str) {
        return (str == null ? "" : str);
    }

    public static String getNoHp(String noHp) {

        if (noHp.substring(0, 1).equals("+")) {
            noHp = noHp.substring(1);
        }
        if (noHp.substring(0, 2).equals("62")) {
            noHp = "0" + noHp.substring(2);
        }

        return noHp;
    }

    public static String getString(Integer str) {
        String ret;
        try {
            ret = Integer.toString(str);
        } catch (Exception e) {
            ret = "";
        }

        return ret;
    }

    public static int getValue(String str) {
        int ret;
        str = str.trim();
        try {
            ret = Integer.parseInt(str);
        } catch (Exception e) {
            ret = 0;
        }
        return ret;
    }

    public static boolean isNumeric(String str) {
        int out;
        boolean ret;
        try {
            out = Integer.parseInt(str);
            ret = true;
        } catch (Exception e) {
            ret = false;
        }

        return ret;
    }

    public static String[] join(String[]... parms) {
        // calculate size of target array
        int size = 0;
        for (String[] array : parms) {
            size += array.length;
        }

        String[] result = new String[size];

        int j = 0;
        for (String[] array : parms) {
            for (String s : array) {
                result[j++] = s;
            }
        }
        return result;
    }

    public static int[] join(int[]... parms) {
        // calculate size of target array
        int size = 0;
        for (int[] array : parms) {
            size += array.length;
        }

        int[] result = new int[size];

        int j = 0;
        for (int[] array : parms) {
            for (int s : array) {
                result[j++] = s;
            }
        }
        return result;
    }

    public static <T> T replaceIfNull(T objectToCheck, T defaultValue) {
        return objectToCheck == null ? defaultValue : objectToCheck;
    }
}
